package com.example.recyclerview_206shj.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.recyclerview_206shj.adapter.MyAdapter;
import com.example.recyclerview_206shj.R;
import com.example.recyclerview_206shj.adapter.TcaseAdapter;
import com.example.recyclerview_206shj.bean.ArticleBean;
import com.example.recyclerview_206shj.bean.TcaseBean;
import com.example.recyclerview_206shj.iface.ArticleListener;
import com.example.recyclerview_206shj.iface.TcaseListener;
import com.example.recyclerview_206shj.model.ArticleModel;
import com.example.recyclerview_206shj.model.TcaseModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//RecyclerView控件
    private RecyclerView.LayoutManager layoutManager;//RecyclerView控件布局管理
    private MyAdapter myAdapter;//RecyclerView的适配器for ArticleBean
    private TcaseAdapter adapterTcase;

    private List<ArticleBean> list = null;//保存数据
    private List<TcaseBean> listTcase = null;

    ArticleListener articleListener = new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            list = beanlist;//拿到文章数据
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, "失败：" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    TcaseListener tcaseListener = new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> beanlist) {
            listTcase = beanlist;//拿到文章数据
            adapterTcase.setList(listTcase);
            adapterTcase.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

//        ArticleModel model = new ArticleModel();
//        model.getResultList("article", 1, "rv1l2tidbomann2qn86nln6ur2", articleListener);
        TcaseModel tcaseModel = new TcaseModel();
        tcaseModel.getResultList("tcase", 1, "rv1l2tidbomann2qn86nln6ur2", tcaseListener);
    }

    //初始化RecyclerView
    private void init(){
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager = new LinearLayoutManager(this);
        //设置线性布局
        recyclerView.setLayoutManager(layoutManager);
        //设置每个item固定高度，提高刷新性能
        recyclerView.setHasFixedSize(true);
        //获取数据
        //list = getData();
        //实例化Adapter
        adapterTcase = new TcaseAdapter(this);
        adapterTcase.setList(listTcase);
        //绑定recyclerview和adapter
        recyclerView.setAdapter(adapterTcase);



//        //实例化Adapter
//        myAdapter = new MyAdapter(this);
//        myAdapter.setList(list);
//        //绑定recyclerview和adapter
//        recyclerView.setAdapter(myAdapter);
    }

    //获取数据源并返回List
//    private List<ArticleBean> getData(){
//        List<ArticleBean> list = new ArrayList<ArticleBean>();
//        ArticleBean articleBean;
//        //第一组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(1);
//        articleBean.setTitle("title1");
//        articleBean.setDescription("desp1");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f1);
//        list.add(articleBean);
//
//        //第二组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(2);
//        articleBean.setTitle("title2");
//        articleBean.setDescription("desp2");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f2);
//        list.add(articleBean);
//
//        //第三组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(3);
//        articleBean.setTitle("title3");
//        articleBean.setDescription("desp3");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f3);
//        list.add(articleBean);
//
//        //第四组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(4);
//        articleBean.setTitle("title4");
//        articleBean.setDescription("desp4");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f4);
//        list.add(articleBean);
//
//        //第五组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(5);
//        articleBean.setTitle("title5");
//        articleBean.setDescription("desp5");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f5);
//        list.add(articleBean);
//
//        //第六组数据
//        articleBean = new ArticleBean();
//        articleBean.setArticleId(6);
//        articleBean.setTitle("title6");
//        articleBean.setDescription("desp6");
//        articleBean.setTime("2017-4-18");
//        articleBean.setImgid(R.drawable.f6);
//        list.add(articleBean);
//
//        return list;
//    }

}